package com.example.ui.fragments;


import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.example.adapter.liveadapter.LiveTitleAdapter;
import com.example.constant.MainUrls;
import com.example.constant.live.BannerBean;
import com.example.constant.live.DataBean;
import com.example.constant.live.EntranceIconsBean;
import com.example.constant.live.Live;
import com.example.constant.live.PartitionsBean;
import com.example.ui.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;

/**
 * 创建时间 2017/9/28
 * 作者 fengxn
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 */

public class LiveFragment extends BaseFragment
{
    public MainUrls t = new MainUrls();
    public List<EntranceIconsBean> entrance;
    public List<PartitionsBean> partitions;
    public List<BannerBean> banner;
    public DataBean dataBean;
    public LiveTitleAdapter adapter;
    public HeadViewHolder hvh;
    @BindView(R.id.rlv_live)
    RecyclerView rlv;
    @BindView(R.id.ll_live)
    LinearLayout llLive;
    private GridLayoutManager manager;


    public LiveFragment()
    {
        // Required empty public constructor
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_live;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        hvh.banner.startTurning(2000);
    }

    @Override
    protected void initData()
    {
        String path = t.getUrls()[0].getUrl();
        HttpRequest.get(path, new BaseHttpRequestCallback<Live>()
        {
            @Override
            public void onStart()
            {
                super.onStart();
            }

            @Override
            public void onSuccess(Live live)
            {
                super.onSuccess(live);
                dataBean = live.getData();
                //banner.addAll(dataBean.getBanner());
                entrance.addAll(dataBean.getEntranceIcons());
                partitions.addAll(dataBean.getPartitions());
                for (int i = 0; i < partitions.size(); i++)
                {
                    Log.i("-----", String.valueOf(partitions.get(i).getPartition().getId()));
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish()
            {
                super.onFinish();
            }

            @Override
            public void onFailure(int errorCode, String msg)
            {
                super.onFailure(errorCode, msg);
            }
        });
    }


    @Override
    protected void initView()
    {
        hvh = new HeadViewHolder();
        llLive.addView(hvh.view);
//        llLive.addView(livemenu);
        banner = new ArrayList<>();
        entrance = new ArrayList<>();
        partitions = new ArrayList<>();
        ButterKnife.bind(this, view);
        manager = new GridLayoutManager(getContext(), 1, LinearLayout.VERTICAL, false);
        rlv.setLayoutManager(manager);
        adapter = new LiveTitleAdapter(partitions, getContext());
        rlv.setAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    public class HeadViewHolder implements CBViewHolderCreator<HeadViewHolder.Convenient>
    {
        public List<BannerBean> bannerList = new ArrayList<>();
        @BindView(R.id.ad_banner)
        ConvenientBanner<BannerBean> banner;
        View view;

        public HeadViewHolder()
        {
            view = LayoutInflater.from(getContext()).inflate(R.layout.item_banner, null);
            ButterKnife.bind(this, view);
            banner.setPages(this, bannerList)
                    //设置圆点
                    .setPageIndicator(new int[]{R.drawable.header_dots_shape_nor, R.drawable.header_dots_shape_sel})
                    //设置原点的位置
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
            loadHeadData();
        }

        private void loadHeadData()
        {
            String path = t.getUrls()[0].getUrl();
            HttpRequest.get(path, new BaseHttpRequestCallback<Live>()
            {
                @Override
                public void onStart()
                {
                    super.onStart();
                }

                @Override
                public void onSuccess(Live live)
                {
                    super.onSuccess(live);
                    dataBean = live.getData();
                    bannerList.addAll(dataBean.getBanner());
                    banner.notifyDataSetChanged();
                }

                @Override
                public void onFinish()
                {
                    super.onFinish();
                }

                @Override
                public void onFailure(int errorCode, String msg)
                {
                    super.onFailure(errorCode, msg);
                }
            });
        }

        @Override
        public Convenient createHolder()
        {
            return new Convenient();
        }


        public class Convenient implements Holder<BannerBean>
        {
            ImageView img;

            @Override
            public View createView(Context context)
            {
                img = new ImageView(context);
                img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                img.setScaleType(ImageView.ScaleType.FIT_XY);
                img.setAdjustViewBounds(true);
                return img;
            }

            @Override
            public void UpdateUI(Context context, int position, BannerBean bannerBean)
            {
                //下载图片
                Picasso.with(context).load(bannerBean.getImg()).into(img);
                //Glide.with(context).load(bannerBean.getImg()).placeholder(R.mipmap.ic_category_t1).error(R.mipmap.ic_category_t1).into(img);
            }
        }
    }
}
